package
{
/*	import com.digitalart.section.features.Features;*/
	import com.digitalart.commons.FontManager;        
	/*import com.digitalart.section.performance.Performance;*/
	
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import com.digitalart.section.index.Index;
	/*import com.digitalart.section.photogallery.GallerySection;*/
	import com.digitalart.commons.CustomEvent;
	import com.digitalart.nav.NavController;
	import flash.net.getClassByAlias;
	import flash.net.registerClassAlias;
	import flash.utils.getQualifiedClassName;
	import flash.events.Event;
	import flash.display.Sprite;
	import com.digitalart.section.Section;
	[SWF (backgroundColor='#000000',  frameRate='40' ) ]
	public class MainController extends Sprite
	{
		
		public static const MAIN_SECTION_ALIAS:String="com.digitalart.section.index::Index";
		//public static const GALLERY_SECTION_ALIAS:String="com.digitalart.section.photogallery::GallerySection";
		//public static const PERFORMACE_SECTION_ALIAS:String="com.digitalart.section.performance::Performance";
		//public static const FEATURES_SECTION_ALIAS:String="com.digitalart.section.features::Features";
		
		private var _nav:NavController;
		private var _currentSection:Section; 
		
		
		public function MainController()
		{
			stage.scaleMode=StageScaleMode.NO_SCALE;
			stage.align=StageAlign.TOP_LEFT;
			
			new FontManager();
			registerSectionTypes();
			loadSeccion(MAIN_SECTION_ALIAS);
		}   
		
		private function registerSectionTypes() : void {                                
			registerClassAlias(MAIN_SECTION_ALIAS,Index);
			/*registerClassAlias(GALLERY_SECTION_ALIAS,GallerySection);
			registerClassAlias(PERFORMACE_SECTION_ALIAS,Performance);
			registerClassAlias(FEATURES_SECTION_ALIAS,Features);     */               
		}
		
		/**********************************************************************/
		
		/**********************************Methods*************************/
		private function addNav() : void {
			
			nav = new NavController();
			nav.addEventListener(NavController.ON_LOAD_COMPLETE, onNavLoadComplete);
			nav.addEventListener(NavController.CHANGE_SECTION_REQUEST, changeSection);
			
		}
		
		private function onNavLoadComplete(event : Event) : void {
			nav.removeEventListener(NavController.ON_LOAD_COMPLETE, onNavLoadComplete);
			this.addChildAt(nav.getView(),1);
			
			
		}
		
		private function changeSection(event : CustomEvent) : void {
			
			loadSeccion(event.data);
		}
		
		public function loadSeccion(className : String) : void {
			trace(getQualifiedClassName(currentSection));
			trace(className);
			if( getQualifiedClassName(currentSection) != className ) {
				trace("CURRENT SECTION " + currentSection);
				var targetClass:Class = getClassByAlias(className) ;
				var newSurrentSection:Section = new targetClass();
				
				currentSection = newSurrentSection;
				currentSection.addEventListener(Section.ON_LOAD_COMPLETE, onSectionLoadComplete);
				
				currentSection.load();
			} 
		}
		
		private function onSectionLoadComplete(event : Event) : void {
			currentSection.removeEventListener(Section.ON_LOAD_COMPLETE, onSectionLoadComplete);
			
			
			if(this.numChildren > 0 ){
				this.removeChildAt(0);                  
			}
			this.addChildAt(currentSection,0);
			
			if(nav == null){
				addNav();
			}
			
		}
		
		/**********************************************************************/
		/**************************Getters & Setters***************************/
		public function get nav() : NavController {
			return _nav;
		}
		
		public function set nav(nav : NavController) : void {
			_nav = nav;
		}
		
		public function get currentSection() : Section {
			return _currentSection;
		}
		
		public function set currentSection(currentSection : Section) : void {
			_currentSection = currentSection;
		}
		/**********************************************************************/
	}
}